package imse.team0.orders.xmladapters;

import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by IntelliJ IDEA.
 * User: Evgenia
 * Date: 5/22/12
 * Time: 4:53 PM
 * To change this template use File | Settings | File Templates.
 */
public class DataAdapter extends XmlAdapter<String, XMLGregorianCalendar> {

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    @Override
    public String marshal(XMLGregorianCalendar v) throws Exception {
        Date date = v.toGregorianCalendar().getTime();
        return ""+date.getTime();
    }

    @Override
    public XMLGregorianCalendar unmarshal(String v) throws Exception {
        GregorianCalendar c = new GregorianCalendar();
        Long timestamp = Long.parseLong(v);
        c.setTime(new Date(timestamp));
        XMLGregorianCalendar date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
        return date2;
    }

}
